//
//  CalibratePopup.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 11/29/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CalibratePopup.h"
#import "CalibrateAccelerometer.h"
#import "TutorialScene.h"
#import "MainGameScene.h"
#import "Setting.h"
#import "Utility.h"

@implementation CalibratePopup

-(id) init
{
    if (self = [super initWithColor:ccc4(0, 0, 0, 0)])
    {
        CCLOG(@"Calibrate popup init");
        
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.2 opacity:255];
        [self runAction:fadeTo];
        
        [self create];
    }
    
    return self;
}

-(id) initWithoutStartFade
{
    if (self = [super initWithColor:ccc4(0, 0, 0, 255)])
    {
        [self create];
    }
    
    return self;
}

-(void) create
{
    [self initMenu];
    [self appear];
    
    mIsTouchEnable = NO;
    self.touchEnabled = YES;
}

-(void) initMenu
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    
    mMenu = [CCMenu menuWithArray:nil];
    [mMenu setPosition:CGPointZero];
    [self addChild:mMenu];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Select position!" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb setPosition:ccp(winSize.width / 2, winSize.height / 2 + uniPos(200, 400))];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:1];
    
    //--------------------------------------------------------------------------------------------------------------------
    CCSprite *sprNormal1 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    CCSprite *sprHover1 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    [sprHover1 setColor:CL_PANEL_BTN];
    CCMenuItem *item1 = [CCMenuItemSprite itemWithNormalSprite:sprNormal1 selectedSprite:sprHover1 target:self selector:@selector(onTopDown)];
    [item1 setOpacity:0];
    [item1 setPosition:ccp(winSize.width / 2, winSize.height / 2 + uniPos(105, 210))];
    
    CCSprite *sprDevice1 = [CCSprite spriteWithSpriteFrameName:SFN_DEVICE];
    [sprDevice1 setPosition:ccp(item1.position.x, item1.position.y - uniPos(20, 40))];
    [sprDevice1 setRotation:15];
    [sprDevice1 setOpacity:0];
    [self addChild:sprDevice1 z:0 tag:11];
    
    CCLabelBMFont *lb1 = [CCLabelBMFont labelWithString:@"Top-down" fntFile:FONT_DEFAULT];
    [lb1 setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb1 setPosition:ccp(item1.position.x, item1.position.y + uniPos(20, 40))];
    [lb1 setOpacity:0];
    [self addChild:lb1 z:0 tag:12];
    
    
    //--------------------------------------------------------------------------------------------------------------------
    CCSprite *sprNormal2 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    CCSprite *sprHover2 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    [sprHover2 setColor:CL_PANEL_BTN];
    CCMenuItem *item2 = [CCMenuItemSprite itemWithNormalSprite:sprNormal2 selectedSprite:sprHover2 target:self selector:@selector(onRegular)];
    [item2 setPosition:ccp(winSize.width / 2, winSize.height / 2 - uniPos(20, 40))];
    [item2 setOpacity:0];
    CCSprite *sprDevice2 = [CCSprite spriteWithSpriteFrameName:SFN_DEVICE];
    [sprDevice2 setPosition:ccp(item2.position.x, item2.position.y - uniPos(20, 40))];
    [sprDevice2 setRotation:45];
    [sprDevice2 setOpacity:0];
    [self addChild:sprDevice2 z:0 tag:21];
    
    CCLabelBMFont *lb2 = [CCLabelBMFont labelWithString:@"Regular" fntFile:FONT_DEFAULT];
    [lb2 setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb2 setPosition:ccp(item2.position.x, item2.position.y + uniPos(20, 40))];
    [lb2 setOpacity:0];
    [self addChild:lb2 z:0 tag:22];
    
    
    
    //--------------------------------------------------------------------------------------------------------------------
    CCSprite *sprNormal3 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    CCSprite *sprHover3 = [CCSprite spriteWithSpriteFrameName:SFN_CIRCLE_200];
    [sprHover3 setColor:CL_PANEL_BTN];
    CCMenuItem *item3 = [CCMenuItemSprite itemWithNormalSprite:sprNormal3 selectedSprite:sprHover3 target:self selector:@selector(onCustom)];
    [item3 setPosition:ccp(winSize.width / 2, winSize.height / 2 - uniPos(145, 290))];
    [item3 setOpacity:0];
    CCSprite *sprDevice3 = [CCSprite spriteWithSpriteFrameName:SFN_DEVICE];
    [sprDevice3 setPosition:ccp(item3.position.x, item3.position.y - uniPos(20, 40))];
    [sprDevice3 setRotation:0];
    [sprDevice3 setOpacity:0];
    [self addChild:sprDevice3 z:0 tag:31];
    
    CCLabelBMFont *lb3 = [CCLabelBMFont labelWithString:@"Custom" fntFile:FONT_DEFAULT];
    [lb3 setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb3 setPosition:ccp(item3.position.x, item3.position.y + uniPos(20, 40))];
    [lb3 setOpacity:0];
    [self addChild:lb3 z:0 tag:32];
    
    CCLabelBMFont *lb4 = [CCLabelBMFont labelWithString:@"?" fntFile:FONT_DEFAULT];
    [lb4 setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb4 setPosition:ccp(item3.position.x, item3.position.y - uniPos(8, 16))];
    [lb4 setOpacity:0];
    [self addChild:lb4 z:0 tag:33];
    
    
    [mMenu addChild:item1 z:0 tag:10];
    [mMenu addChild:item2 z:0 tag:20];
    [mMenu addChild:item3 z:0 tag:30];
}

-(void) calibrate
{
    [self removeAllChildren];
    
    
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    
    CCLabelBMFont *lb2 = [CCLabelBMFont labelWithString:@"Tap anywhere to continue!" fntFile:FONT_DEFAULT];
    [lb2 setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb2 setPosition:ccp(winSize.width / 2, winSize.height / 2 - uniPos(25, 50))];
    [lb2 setOpacity:0];
    [self addChild:lb2 z:0 tag:101];
    
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:@"Hold your device comfor in 3 seconds!" fntFile:FONT_DEFAULT];
    [lb setAnchorPoint:ccp(0.5f, 0.5f)];
    [lb setPosition:ccp(winSize.width / 2, winSize.height / 2)];
    [lb setOpacity:0];
    [self addChild:lb z:0 tag:100];
    
    CCDelayTime *d1 = [CCDelayTime actionWithDuration:1];
    CCFadeTo *f1 = [CCFadeTo actionWithDuration:1 opacity:255];
    CCDelayTime *d2 = [CCDelayTime actionWithDuration:2];
    CCFadeTo *f2 = [CCFadeTo actionWithDuration:1 opacity:0];
    CCCallBlock *c1 = [CCCallBlock actionWithBlock:^{
        [lb setString:@"3"];
        [lb setOpacity:255];
        [lb setScale:0];
        
        [[CalibrateAccelerometer getInstance] setType:eAT_CUSTOM];
        self.accelerometerEnabled = YES;
    }];
    CCScaleTo *s1 = [CCScaleTo actionWithDuration:0.2 scale:2];
    CCEaseBackOut *e1 = [CCEaseBackOut actionWithAction:s1];
    CCDelayTime *d11 = [CCDelayTime actionWithDuration:0.8];
    CCCallBlock *c2 = [CCCallBlock actionWithBlock:^{
        [lb setString:@"2"];
        [lb setOpacity:255];
        [lb setScale:0];
    }];
    CCScaleTo *s2 = [CCScaleTo actionWithDuration:0.2 scale:2];
    CCEaseBackOut *e2 = [CCEaseBackOut actionWithAction:s2];
    CCDelayTime *d22 = [CCDelayTime actionWithDuration:0.8];
    CCCallBlock *c3 = [CCCallBlock actionWithBlock:^{
        [lb setString:@"1"];
        [lb setOpacity:255];
        [lb setScale:0];
    }];
    CCScaleTo *s3 = [CCScaleTo actionWithDuration:0.2 scale:2];
    CCEaseBackOut *e3 = [CCEaseBackOut actionWithAction:s3];
    CCDelayTime *d33 = [CCDelayTime actionWithDuration:0.8];
    CCCallBlock *c4 = [CCCallBlock actionWithBlock:^{
        [[CalibrateAccelerometer getInstance] averageOriginAngel];
        [lb setOpacity:0];
        [lb setScale:1];
//        [lb setString:@"Done!"];
        // for test
//        [lb setString:[NSString stringWithFormat:@"Done! : %f", [CalibrateAccelerometer getInstance]._originAngle]];
        [lb setString:@"Done!"];
        [lb runAction:[self getFadeTo:255 withDelay:0]];
        [lb2 runAction:[self getFadeTo:255 withDelay:0]];
        mIsTouchEnable = YES;
        self.accelerometerEnabled = NO;
    }];
    
    CCSequence *seq = [CCSequence actions:d1, f1, d2, f2, c1, e1, d11, c2, e2, d22, c3, e3, d33, c4, nil];
    [lb runAction:seq];
    
    
}

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    [[CalibrateAccelerometer getInstance] addSumCalibrate:acceleration];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (!mIsTouchEnable)
        return;
    
    
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.2 opacity:0];
    CCCallBlock *call = [CCCallBlock actionWithBlock:^{
        [self nextScene];
    }];
    CCSequence *seq = [CCSequence actions:fadeTo, call, nil];
    [self runAction:seq];
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:TOUCH_PRIORITY_RESULT_POPUP swallowsTouches:YES];
}


-(void) onTopDown
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    [[CalibrateAccelerometer getInstance] setType:eAT_TOP_DOWN];

    [self disappear];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1.0];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.2 opacity:0];
    CCCallBlock *call = [CCCallBlock actionWithBlock:^{
        [self nextScene];
    }];
    CCSequence *seq = [CCSequence actions:delay, fadeTo, call, nil];
    [self runAction:seq];
}

-(void) onRegular
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    [[CalibrateAccelerometer getInstance] setType:eAT_REGULAR];
    
    [self disappear];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1.0];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.2 opacity:0];
    CCCallBlock *call = [CCCallBlock actionWithBlock:^{
        [self nextScene];
    }];
    CCSequence *seq = [CCSequence actions:delay, fadeTo, call, nil];
    [self runAction:seq];
}

-(void) onCustom
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    [self disappear];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1];
    CCCallBlock *call = [CCCallBlock actionWithBlock:^{
        [self calibrate];
    }];
    CCSequence *seq = [CCSequence actions:delay, call, nil];
    [self runAction:seq];
}

-(void) nextScene
{
    [Setting getInstance]._isCalibrate = YES;
    [self removeFromParent];
}

-(void) onExit
{
    [super onExit];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CalibratePopupClose" object:nil];
}

-(CCSequence *) getFadeTo:(int)opacity withDelay:(float)delay
{
    CCDelayTime *delayTime = [CCDelayTime actionWithDuration:delay];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.5 opacity:opacity];
    CCSequence *sequence = [CCSequence actions:delayTime, fadeTo, nil];

    return sequence;
}

-(void) appear
{
    float timeDelay = 0.2;
    
    CCNode *obj01 = [self getChildByTag:1];
    [obj01 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    
    timeDelay += 0.1;
    CCNode *obj11 = [mMenu getChildByTag:10];
    CCNode *obj12 = [self getChildByTag:11];
    CCNode *obj13 = [self getChildByTag:12];
    [obj11 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj12 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj13 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    
    timeDelay += 0.1;
    CCNode *obj21 = [mMenu getChildByTag:20];
    CCNode *obj22 = [self getChildByTag:21];
    CCNode *obj23 = [self getChildByTag:22];
    [obj21 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj22 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj23 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    
    timeDelay += 0.1;
    CCNode *obj31 = [mMenu getChildByTag:30];
    CCNode *obj32 = [self getChildByTag:31];
    CCNode *obj33 = [self getChildByTag:32];
    CCNode *obj34 = [self getChildByTag:33];
    [obj31 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj32 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj33 runAction:[self getFadeTo:255 withDelay:timeDelay]];
    [obj34 runAction:[self getFadeTo:255 withDelay:timeDelay]];
}

-(void) disappear
{
    float timeDelay = 0.5;
    
    CCNode *obj01 = [self getChildByTag:1];
    [obj01 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    
    timeDelay -= 0.1;
    CCNode *obj11 = [mMenu getChildByTag:10];
    CCNode *obj12 = [self getChildByTag:11];
    CCNode *obj13 = [self getChildByTag:12];
    [obj11 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj12 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj13 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    
    timeDelay -= 0.1;
    CCNode *obj21 = [mMenu getChildByTag:20];
    CCNode *obj22 = [self getChildByTag:21];
    CCNode *obj23 = [self getChildByTag:22];
    [obj21 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj22 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj23 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    
    timeDelay -= 0.1;
    CCNode *obj31 = [mMenu getChildByTag:30];
    CCNode *obj32 = [self getChildByTag:31];
    CCNode *obj33 = [self getChildByTag:32];
    CCNode *obj34 = [self getChildByTag:33];
    [obj31 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj32 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj33 runAction:[self getFadeTo:0 withDelay:timeDelay]];
    [obj34 runAction:[self getFadeTo:0 withDelay:timeDelay]];
}

-(void) dealloc
{
    CCLOG(@"Calibrate popup dealloc!");
    [super dealloc];
}

@end
